<?php
/**
 * Created by PhpStorm.
 * User: madong
 * Date: 2018/10/16
 * Time: 9:34 AM
 */

namespace app\api\controller\jiaoyi;

use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Qianbao extends Controller
{
    //获取场外指定钱包余额

    public function Get_cw_balance(){

        $res = Request::instance();

        $uname = $this->Jiemi($res->param('uname'));

        $coin_type = $this->Jiemi($res->param('coin_type'));

        if (!$uname&&!$coin_type){echo json_encode(['zt'=>0,'msg'=>'解密出现异常！不要在尝试']);die();}

        $data = [

            'keyong' => Db::name('jiaoyi_uinfo')->where('uname',$uname)->value($coin_type),

            'dongjie' => Db::name('jiaoyi_uinfo')->where('uname',$uname)->value('d'.$coin_type)

        ];

        if ($data){

            echo json_encode(['zt'=>1,'msg'=>'获取完成','data'=>$this->Jiami(json_encode($data))]);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'获取失败']);

        }

    }

    //获取真实钱包指定币种的余额
    //根据账号查询当前账户的实际余额(接口查询)

    public function Get_yuerzhen()
    {

        $res = Request::instance();

        $user_address = Db::name('adeth')->where('uname', $res->param('uname'))->value('address');

        if ($res->param('coin_type') == 'eth') {

            $balance = json_decode(file_get_contents('http://47.92.195.55:8883/?type=4&account=' . $user_address))->Balance;

        } else {

            $balance = json_decode(file_get_contents('http://47.92.195.55:8882/?type=token_balance&con_addr=' . Config::get('con_' . $res->param('coin_type')) . '&account=' . $user_address))->balance;

        }

        echo json_encode(['zt'=>1,'msg' => $balance]);

    }

    //查询指定币种的数据库真实余额做预加载用
    public function Get_data_yuer(){

        $res = Request::instance();

        $data = Db::name('adeth')->where('uname',$res->param('uname'))->value($res->param('coin_type'));

        if ($data){

            echo json_encode(['zt'=>1,'msg'=>'获取成功','data'=>$data]);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'获取失败','data'=>0.0000]);

        }

    }
    //数据解密
    private function Jiemi($data){

        $jiami_data = openssl_decrypt(base64_decode($data,false),"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV'));

        return $jiami_data;

    }
    //数据加密
    private function Jiami($data){

        $jiemi_data =  base64_encode(openssl_encrypt($data,"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV')));

        return $jiemi_data;

    }

}